﻿<%@ WebHandler Language="C#" Class="Download" %>

using System;
using System.Web;

public class Download : IHttpHandler {
    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";

        if (string.IsNullOrEmpty(context.Request["file"]))
            context.Response.Write("此附件不存在！");

        try
        {
            string strFilename = context.Request["file"].ToString();
            string strFilePath = context.Server.MapPath(Chenxin.Lib.Utility.Constants.DIRECTORY + strFilename);

            if (System.IO.File.Exists(strFilePath))
            {
                strFilename = context.Server.UrlDecode(strFilePath);
                if (System.IO.File.Exists(strFilename))
                {
                    System.IO.FileInfo fi = new System.IO.FileInfo(strFilename);
                    context.Response.Clear();
                    context.Response.ClearHeaders();
                    context.Response.Buffer = false;
                    
                    context.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(System.IO.Path.GetFileName(strFilename), System.Text.Encoding.UTF8));
                    context.Response.AppendHeader("Content-Length", fi.Length.ToString());
                    context.Response.ContentType = "application/octet-stream";
                    context.Response.WriteFile(strFilename);
                    context.Response.Flush();
                    context.Response.End();
                }
                else
                {
                    context.Response.Write("此附件不存在！");
                }

            }
            else
            {
                context.Response.Write("此附件不存在！");
            }
        }
        catch (Exception ex)
        {
            context.Response.Write("此附件不存在！");
        }

    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}